package com.decent.manager.system.dao.provider;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.EnumUtil;
import com.decent.manager.system.dto.SystemOrderDTO;

import java.util.Map;
import java.util.Objects;

/**
 * @author: decent Gjh
 * @date: 2022/11/02/9:42
 * @description:
 */
public class SystemOrderDaoProvider{
    public String list(Map<String, SystemOrderDTO> params) {
        SystemOrderDTO dto = params.get("order");
        StringBuilder sql = new StringBuilder(256);
        sql.append("select * from common_order where 1 = 1 ");
        // 订单状态
        if (Objects.nonNull(dto.getOrderState())&&EnumUtil.isEnum(dto.getOrderState())) {
            sql.append(" and order_state = #{order.orderState} ");
        }
        // 订单编号
        if (CharSequenceUtil.isNotBlank(dto.getOrderUuid())) {
            sql.append(" and order_uuid = #{order.orderUuid} ");
        }
        // 用户名称
        if (CharSequenceUtil.isNotBlank(dto.getUserName())){
            sql.append(" and user_id in (SELECT id FROM web_user WHERE name = #{order.userName} ) ");
        }
        // 商品 品牌 分类
        if (Objects.nonNull(dto.getGoodsId()) && dto.getGoodsId()!=0){
            sql.append(" and good_id = #{order.goodsId} ");
        }else if (Objects.nonNull(dto.getBrandId()) && dto.getBrandId()!=0){
            sql.append(" and brand_id = #{order.brandId} ");
        }else if (Objects.nonNull(dto.getClassifyId()) && dto.getClassifyId()!=0){
            sql.append(" and classify_id = #{order.classifyId} ");
        }
        //时间
        if (Objects.nonNull(dto.getStartTime()) && Objects.nonNull(dto.getEndTime())){
            sql.append(" and create_datetime BETWEEN #{order.startTime} AND #{order.endTime} ");
        }
        sql.append(" order by create_datetime ");
        return sql.toString();
    }
}
